package com.activenetwork.smlite.page.event.EventDetailView;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import com.activenetwork.custom.uielement.UIElement;
import com.activenetwork.custom.uielement.decorate.InJson;
import com.activenetwork.schema.model.rfp.RfpBasicType;
import com.activenetwork.schema.model.rfp.RfpType;
import com.activenetwork.smlite.page.generic.EventModelPage;
import com.activenetwork.smlite.page.rfp.RfpMaintenanceView.RfpSetting;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.grid.NoMatchRowException;
import com.activenetwork.util.grid.OperatableGrid;
import com.activenetwork.util.grid.condition.ConditionBuilder;
import com.activenetwork.util.grid.condition.EqualCondition;
import com.activenetwork.util.grid.gson.GridFactory;

public class EventSourceSubview extends EventModelPage {
	
	@InJson
	private UIElement backToBasic;
	@InJson
	private UIElement addRfp;
	@InJson
	private UIElement deleteRfp;
	@InJson
	private UIElement copyRfp;
	
	private final OperatableGrid grid = GridFactory.getGrid("EventSourceSubview", "SearchRfpGrid");
	
	/**
	 * Click add rfp button
	 * @return
	 */
	public RfpSetting clickAddRfp(){
		addRfp.click();
		return PageLoader.load(RfpSetting.class);
	}
	/**
	 * Click Delete Rfp
	 * @return
	 */
	public EventSourceSubview clickDeleteRfp(){
		deleteRfp.click();
		return PageLoader.load(EventSourceSubview.class);
	}
	/**
	 * Click Basic button to go back event basic page
	 * @return
	 */
	public EventBasicSubview goBackBasicSubview(){
		backToBasic.click();
		return PageLoader.load(EventBasicSubview.class);
	}
	/**
	 * Kill the popup
	 * @param ok
	 * @return
	 */
	public EventSourceSubview confirmPopup(boolean ok){
		WebDriver driver = EnvContext.webDriverTL.get();
		if(ok){
			driver.findElement(By.cssSelector(".gwt-PopupPanel div.modal-footer button.btn-orange")).click();
		}else{
			driver.findElement(By.cssSelector(".gwt-PopupPanel div.modal-footer button.btn:first-child")).click();
		}
		return PageLoader.load(EventSourceSubview.class);
	}
	/**
	 * Click copy rfp button
	 */
	public void clickCopyRfp(){
		copyRfp.click();
	}
	
	/**
	 * Click one specific rfp name link from the grid
	 * Search with rfp name data object
	 * @param rfp
	 * @return
	 * @throws NoMatchRowException
	 */
	public RfpSetting selectRfpFromGrid(RfpType rfp) throws RuntimeException{
		ConditionBuilder builder = new ConditionBuilder();
		builder.add(new EqualCondition(), 2, rfp.getBasic().getRfpName());
		try{
			grid.clickRow(2, builder.build());
		}catch(NoMatchRowException e){
			throw new RuntimeException("Nested NoMatchException: " + e.getMessage());
		}
		return PageLoader.load(RfpSetting.class);	
		
	}
	/**
	 * Click one specific rfp name link from the grid
	 * Search with rfp name string
	 * @param rfpName
	 * @return
	 * @throws NoMatchRowException
	 */
	public RfpSetting selectRfpFromGrid(String rfpName) throws RuntimeException{
		RfpType rfp = new RfpType();
		RfpBasicType rfpBasic = new RfpBasicType();
		rfpBasic.setRfpName(rfpName);
		rfp.setBasic(rfpBasic);
		return selectRfpFromGrid(rfp);
	}
	/**
	 * Check on the specific rfp record in the grid
	 * Check by rfp data type
	 * @param rfp
	 * @return
	 * @throws RuntimeException
	 */
	public EventSourceSubview checkRfpFromGrid(RfpType rfp) throws RuntimeException{
		ConditionBuilder builder = new ConditionBuilder();
		builder.add(new EqualCondition(), 2, rfp.getBasic().getRfpName());
		try{
			grid.checkOnRow(builder.build());
		}catch(NoMatchRowException e){
			throw new RuntimeException("Nested NoMatchException: " + e.getMessage());
		}
		return this;
	}
	/**
	 * Check on the specific rfp record in the grid
	 * Check by rfp name string
	 * @param rfp
	 * @return
	 * @throws RuntimeException
	 */
	public EventSourceSubview checkRfpFromGrid(String rfpName) throws RuntimeException{
		RfpType rfp = new RfpType();
		RfpBasicType rfpBasic = new RfpBasicType();
		rfpBasic.setRfpName(rfpName);
		rfp.setBasic(rfpBasic);
		return checkRfpFromGrid(rfp);
	}
	

}
